﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UserCenter.Console.Sdk.Demo.Tools
{
    public class SmsHelper
    {
        public static (bool Success, string Message) Send(string phone, string code)
        {
            IClientProfile profile = DefaultProfile.GetProfile("default", "LTAIVW0nVneU4jqi", "DKIafomTWk0eFpY3Z6WnaqqbBDVktH");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", phone);
            request.AddQueryParameters("TemplateCode", "SMS_144695180");
            request.AddQueryParameters("SignName", "华商情");
            request.AddQueryParameters("TemplateParam", $"{{\"code\":\"{code}\"}}");
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                string jsonStr = System.Text.Encoding.Default.GetString(response.HttpResponse.Content);
                JObject jsonObj = JObject.Parse(jsonStr);
                if (jsonObj["Code"].Equals("OK"))
                {
                    return (true, "");
                }
                else
                {
                    return (false, jsonObj["Message"].ToString());
                }
            }
            catch (Exception e)
            {
                return (false, "发送失败");
            }
        }
    }
}